<template>
	<div
		class="my-content-layers-list-item"
		:class="{ hover, select, 'list-mini': $store.chart.layerType === 'text' }"
		@mousedown="mousedownHandle($event, componentData)"
		@mouseenter="mouseenterHandle(componentData)"
		@mouseleave="mouseleaveHandle(componentData)"
		@contextmenu="handleContextMenu($event, componentData, optionsHandle)"
	>
		<div class="flex-center item-content">
			<el-image
				v-if="componentData.orgConfig.image"
				class="list-img"
				fit="contain"
				:src="componentData.orgConfig.image"
				loading="lazy"
			/>
			<my-icon v-else :icon="componentData.orgConfig.icon || 'Postcard'" class="list-img f-s-30" color="#999" />
			<el-text class="list-text f1 w0" truncated> {{ componentData?.title }}</el-text>
			<layers-status :isGroup="isGroup" :hover="hover" :status="componentData.status" />
		</div>
		<div :class="{ 'select-modal': select }"></div>
	</div>
	<n-collapse-transition :show="expend" v-if="componentList.length">
		<div class="my-content-layers-group-list-item">
			<!-- 单组件 -->
			<layers-list-item v-for="element in componentList" :key="element.id" :componentData="element" />
		</div>
	</n-collapse-transition>
</template>

<script setup name="LayersListItem">
import LayersStatus from './LayersStatus.vue';

const props = defineProps({
	componentData: {
		type: Object,
		required: true
	},
	isGroup: {
		type: Boolean,
		default: false
	}
});

const select = computed(() => {
		const id = props.componentData.id;
		return $store.chart.targetChart.selectId.find((e) => e === id);
	}),
	// 悬浮对象
	hover = computed(() => {
		return props.componentData.id === $store.chart.targetChart.hoverId;
	}),
	componentList = computed(() => {
		return cloneDeep(
			$store.chart.componentList.filter((item) => item?.parentId?.includes?.(props.componentData.id))
		).reverse();
	});

/**********************************************************左右键功能******************************/
import { handleContextMenu } from '../../hooks/useContextMenu.hook';
import { MenuEnum } from '@/enums/editPageEnum';
const expend = ref(false);
// 右键事件
function optionsHandle(targetList, allList, targetInstance) {
	// 多选处理
	if ($store.chart.targetChart.selectId.length > 1) {
		return targetList.filter((i) => i.key === MenuEnum.GROUP);
	}
	const statusMenuEnums = [];
	// 处理锁定与隐藏
	if (targetInstance.status.lock) {
		statusMenuEnums.push(MenuEnum.LOCK);
	} else {
		statusMenuEnums.push(MenuEnum.UNLOCK);
	}
	if (targetInstance.status.hide) {
		statusMenuEnums.push(MenuEnum.HIDE);
	} else {
		statusMenuEnums.push(MenuEnum.SHOW);
	}
	return targetList.filter((item) => !statusMenuEnums.includes(item.key));
}
// 点击事件
function mousedownHandle(e, item) {
	// 若此时按下了 CTRL, 表示多选
	const id = item.id;
	if (e.buttons === 1 && $store.chart.KeyboardActive?.ctrl) {
		// 若已选中，则去除
		if ($store.chart.targetChart.selectId.includes(id)) {
			const exList = $store.chart.targetChart.selectId.filter((e) => e !== id);
			$store.chart.setTargetSelectChart(exList);
		} else {
			$store.chart.setTargetSelectChart(id, true);
		}
		return;
	}
	$store.chart.setTargetSelectChart(id);
	expend.value = !expend.value;
}
// 进入事件
function mouseenterHandle(item) {
	$store.chart.setTargetHoverChart(item.id);
}
// 移出事件
function mouseleaveHandle() {
	$store.chart.setTargetHoverChart(undefined);
}
</script>

<style lang="scss" scoped>
$centerHeight: 52px;
$centerMiniHeight: 28px;
$textSize: 10px;

@include b(content-layers-group-list-item) {
	position: relative;
	margin: 10px 0 5px 5%;
	.my-content-layers-list-item {
		margin-right: 0 !important;
		width: 90% !important;
	}
}

@include b(content-layers-list-item) {
	position: relative;
	height: $centerHeight;
	width: 90%;
	margin: 5px 5%;
	margin-bottom: 5px;
	border-radius: 5px;
	cursor: pointer;
	border: 1px solid rgba(0, 0, 0, 0);
	transition: all 0.2s;
	&.hover,
	&:hover {
		background-color: var(--color-bg-4);
	}
	&:hover {
		:deep() {
			.icon-item {
				opacity: 1;
			}
		}
	}
	.item-content {
		position: absolute;
		top: 0;
		left: 0;
		z-index: 1;
		padding: 6px 5px;
		width: 100%;
		height: 100%;
		box-sizing: border-box;
	}

	.select-modal {
		width: 100%;
		height: 100%;
		opacity: 0.3;
		background-color: var(--el-color-primary);
	}

	.list-img {
		flex-shrink: 0;
		height: 100%;
		overflow: hidden;
		border: none !important;
		border-color: var(--el-color-primary-light-7);
	}

	.list-text {
		padding-left: 6px;
		font-size: $textSize;
	}

	/* 选中样式 */
	&.select {
		border: 1px solid var(--el-color-primary);
		background-color: rgba(0, 0, 0, 0);
	}

	// mini样式
	&.list-mini {
		height: $centerMiniHeight;
		.item-content {
			padding: 2px;
		}
	}
}
</style>
